package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.WorkOrderConfig;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.service.business.WorkOrderConfigService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * @author ：hzw
 * @date ：Created in 2021/8/23 14:47
 * @description：工单设置接口层
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping(value = "/WOConfig")
public class WorkOrderConfigController extends BaseController {
    @Autowired
    private WorkOrderConfigService workOrderConfigService;
    @Autowired
    private OperaPswService operaPswService;

    @RequestMapping(value = "/edit")
    @OperateLog(module = ModuleEnum.WORKORDER, methods = MethodEnum.WORKORDER_EDIT)
    public JsonResult edit(WorkOrderConfig workOrderConfig, String operaPsw, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            workOrderConfig.setAccountId(loginInfo.id);
        } else {
            Assert.notNull(workOrderConfig.getAccountId(), "ver.accountId.notNull");
        }
        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_OPERATION_CONFIG.getModule(), operaPsw);

        workOrderConfigService.edit(loginInfo, workOrderConfig);

        return JsonResult.ok();
    }

    //获取运营区域配置
    @RequestMapping("/getByAccount")
    public JsonResult getByAccount(@NotNull(message = "ver.accountId.notNull") Integer accountId, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);

        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId.notNull");
        }

        WorkOrderConfig workOrderConfig = workOrderConfigService.getConfig(accountId, loginInfo);
        /**设置默认值，全部默认设置未开启*/
        if (workOrderConfig == null) {
            workOrderConfig = new WorkOrderConfig()
                    .setAccountId(accountId)
                    .setPowerChangeSwitch(0)
                    .setPowerChangeNumber(0)
                    .setMoveSwitch(0)
                    .setMoveNumber(0)
                    .setRepairSwitch(0)
                    .setRepairNumber(0)
                    .setRedeploySwitch(0)
                    .setRemark(null)
                    .setDispatchSwitch(0)
                    .setDispatchNumber(120)
                    .setOutSiteMoveSwitch(0)
                    .setOutSiteMoveNumber(120)
                    .setSuperZoneMoveSwitch(0)
                    .setSuperZoneMoveNumber(120)
                    .setClockInOrderSwitch(0);
        }
        return JsonResult.ok(workOrderConfig);
    }

}
